package studentsinfomanagement.dao;

import studentsinfomanagement.bean.Department;

import java.sql.SQLException;

/**
 * 专业数据类
 */
public class DepartmentDAO extends BaseDAO {

    private static DepartmentDAO departmentDAO = null;

    /**
     * 单例模式
     */
    private DepartmentDAO(){
    }

    /**
     * 获取DepartmentDAO的实例
     * @return 实例
     */
    public static DepartmentDAO getInstance(){
        if(departmentDAO == null){
            departmentDAO = new DepartmentDAO();
        }
        return departmentDAO;
    }

    /**
     * 根据专业名称获得专业信息
     * @param name 专业名称
     * @return 专业对象
     */
    public Department getDepartmentByName(String name){
        //准备sql查询语句
        String checkSql = "select * from department where name=?";
        //准备参数
        String[] checkParam = { name };
        //调用数据库工具类的查询方法，执行查询语句
        rs = db.executeQuery(checkSql, checkParam);
        Department result = null;
        try {
            if(rs.next()){
                result = new Department();
                result.setDno(rs.getString("dno"));
                result.setName(rs.getString("name"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return result;
    }

    /**
     * 根据专业号获得专业信息
     * @param dno 专业名称
     * @return 专业对象
     */
    public Department getDepartmentByDno(String dno){
        //准备sql查询语句
        String checkSql = "select * from department where dno=?";
        //准备参数
        String[] checkParam = { dno };
        //调用数据库工具类的查询方法，执行查询语句
        rs = db.executeQuery(checkSql, checkParam);
        Department result = null;
        try {
            if(rs.next()){
                result = new Department();
                result.setDno(rs.getString("dno"));
                result.setName(rs.getString("name"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return result;
    }
}
